package com.samoiloff.ui.list.smart.model {
	import com.samoiloff.framework.model.AbstractModel;
	import com.samoiloff.ui.list.smart.SmartList;
	import com.samoiloff.ui.list.smart.interfaces.CellRenderer;
	import com.samoiloff.ui.list.smart.view.SmartListView;

	import flash.events.Event;

	/**
	 * @author Samoiloff
	 */
	public class SmartListModel extends AbstractModel {
		
		public static const DATA_PROVIDER_CHANGED	: String = "dataProviderChanged";
		public static const CELL_RENDERER_CHANGED	: String = "cellRendererChanged";
		public static const LAYOUT_CHANGED			: String = "layoutChanged";
		public static const SELECTED_INDEX_CHANGED	: String = "selectedIndexChanged";
		public static const CLONE_EVENTS_CHANGED	: String = "cloneEventsChanged";
		public static const RENDER					: String = "render";
		
		
		private var _minWidth	: Number = 40;
		private var _minHeight	: Number = 40;
		
		public function SmartListModel(component:SmartList, view:SmartListView) {
			super(component, view);
			
			_width	= 208;
			_height	= 295;

			_layout = new SmartListLayout(_view.mainMc);
			_cellRenderer	= new SmartListCellRenderer();
			
			_selectedIndex = -1;
			
			complete();
		}
		
		public function get view(): SmartListView {
			return _view as SmartListView;
		}
		
		/*
		 * width
		 */
		private var _width			: Number;
		
		public function get width(): Number {
			return _width;
		}
		
		public function set width(width : Number) : void {
			width = width < _minWidth ? _minWidth : width;
			_width = width;
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		/*
		 * height
		 */
		
		private var _height			: Number;
		
		public function get height(): Number {
			return _height;
		}
		
		public function set height(height : Number) : void {
			height = height < _minHeight ? _minHeight : height;
			_height = height;
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		/*
		 * dataProvider
		 */
		
		private var _dataProvider	: Array;
		
		public function get dataProvider(): Array {
			return _dataProvider;
		}
		
		public function set dataProvider(dataProvider: Array): void {
			_dataProvider = dataProvider;
			dispatchEvent(new Event(DATA_PROVIDER_CHANGED));
		}
		
		/*
		 * cellRenderer
		 */
		private var _cellRenderer	: CellRenderer;
		
		public function get cellRenderer(): CellRenderer {
			return _cellRenderer;
		}
		
		public function set cellRenderer(cellRenderer: CellRenderer): void {
			_cellRenderer = cellRenderer;
			dispatchEvent(new Event(CELL_RENDERER_CHANGED));
		}
		
		/*
		 * layout
		 */
		
		private var _layout			: SmartListLayout;
		
		public function get layout(): SmartListLayout {
			return _layout;
		}
		
		public function set layout(layout: SmartListLayout): void {
			_layout = layout;
			dispatchEvent(new Event(LAYOUT_CHANGED));
		}
		
		/*
		 * selected index
		 */
		private var _selectedIndex: int;
		
		public function get selectedIndex(): int {
			return _selectedIndex;
		}
		
		public function set selectedIndex(selectedIndex: int): void {
			if (selectedIndex != _selectedIndex) {
				_selectedIndex = selectedIndex;
				dispatchEvent(new Event(SELECTED_INDEX_CHANGED));
			}
		}

		private var _cloneEvents : Array;
		
		/*
		 * Clone events. Events that are redispatched
		 */
		public function get cloneEvents() : Array {
			return _cloneEvents;
		}

		public function set cloneEvents(cloneEvents : Array) : void {
			_cloneEvents = cloneEvents;
			dispatchEvent(new Event(CLONE_EVENTS_CHANGED));
		}
		
		public function render() : void {
			dispatchEvent(new Event(RENDER));
		}
		
	}
}
